using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;

public class Card : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    [Header("组件")]
    public SpriteRenderer cardSprite,cardLevelSprite;
    public TextMeshPro nameText,costTxet,descriptionText,typeText;
    [Header("原始数据")]
    public Vector3 originalPosition;
    public Quaternion originalRotation;
    public int originalLayerOrder;
    
    public CardDataDetails cardDataSO;

    public bool isAnimating;
    public bool isAvailable;
    
    public Player player;
    [Header("广播事件")]
    public ObjectEventSO discardCardEvent;

    public IntEventSO costEvent;

    private void Start()
    {
        Init(cardDataSO);
    }

    public void Init(CardDataDetails data)
    {
        cardDataSO = data;
        cardSprite.sprite = cardDataSO.cardImage;
        costTxet.text = cardDataSO.cardCost.ToString();
        descriptionText.text = cardDataSO.cardDescription;
        typeText.text = cardDataSO.cardType switch
        {
            CardType.Attack => "攻击",
            CardType.Defense => "技能",
            CardType.Abilities => "能力",
            _ => throw new ArgumentOutOfRangeException()
        };
        
        if (cardDataSO.isPrimaryCard)
        {
            nameText.text = "<color=\"black\">" + "初级·" + cardDataSO.cardName + "</color>";
            cardLevelSprite.color = Color.white;
        }
        else if (cardDataSO.isIntermediateCard)
        {
            nameText.text = "<color=\"blue\">" + "中级·" + cardDataSO.cardName + "</color>";
            cardLevelSprite.color = Color.green;
        }
        else if (cardDataSO.isSeniorCard)
        {
            nameText.text = "<color=\"purple\">" + "高级·" + cardDataSO.cardName + "</color>";
            cardLevelSprite.color = Color.red;
        }
        
        player = GameObject.FindWithTag("Player").GetComponent<Player>();
    }
    
    public void UndatePositionAndRotation(Vector3 position,Quaternion rotation)
    {
        originalPosition = position;
        originalRotation = rotation;
        //transform.position = position;
        //transform.rotation = rotation;
        originalLayerOrder = GetComponent<SortingGroup>().sortingOrder;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (isAnimating) return;
        //transform.GetChild(0).position = originalPosition + Vector3.up;
        // transform.position = new Vector3(originalPosition.x, -3.5f, originalPosition.z);
        // transform.rotation = Quaternion.identity;
        transform.SetPositionAndRotation(new Vector3(originalPosition.x, -3.5f, originalPosition.z), Quaternion.identity);
        GetComponent<SortingGroup>().sortingOrder = 20;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (isAnimating) return;
        ResetCardTransform();
    }
    
    public void ResetCardTransform()
    {
        transform.SetPositionAndRotation(originalPosition, originalRotation);
        GetComponent<SortingGroup>().sortingOrder = originalLayerOrder;
        isAnimating = false;
    }

    public void ExcuteCardEffects(CharacterBase from, CharacterBase target)
    {
        //减少能力，通知回收卡牌
        costEvent.RaisEvent(cardDataSO.cardCost,this);
        discardCardEvent.RaisEvent(this,this);
        
        cardDataSO.effects.Excute(from, target);

    }
    
    public void UpdateCardState()
    {
        //判断卡牌能量
        isAvailable = cardDataSO.cardCost <= player.CurrentMana;
        costTxet.color = isAvailable ? Color.green : Color.red;
    }
}
